package com.environment_pro.monitor.filter;

import org.apache.log4j.Logger;

import com.environment_pro.monitor.util.StaticUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 
 * @author 
 *
 */

public class SystemFilter implements Filter {

    Logger logger = Logger.getLogger(SystemFilter.class);

    /**
     * 判断是否为Ajax请求
     *
     * @param request HttpServletRequest
     * @return 是true, 否false
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
    	return request.getRequestURI().indexOf("/api")>-1;
    }


    public void init(FilterConfig filterConfig) throws ServletException {
    	

    }


    public void destroy() {
        //To change body of implemented methods use File | Settings | File Templates.
    }


	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;  
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;  
        HttpSession session = httpRequest.getSession(true);
        String loginname  = StaticUtil.null2String(session.getAttribute("loginname"));
		if (loginname!=null && loginname.length()>1) { 
			logger.info(loginname);
			//httpResponse.sendRedirect("user/index"); 
        } else {  
        	httpResponse.sendRedirect("login.jsp"); // 返回提示页面  
        } 
        //filterChain.doFilter(servletRequest, servletResponse);  
	}


}
